package myky.com.doctorapp.fragment


import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import io.reactivex.Observable
import kotlinx.android.synthetic.main.fragment_persion_info.*
import myky.com.doctorapp.R
import myky.com.doctorapp.base.BaseFragment
import myky.com.doctorapp.contract.HealthPersonInfoContract
import myky.com.doctorapp.entity.HealthRecordEntity
import myky.com.doctorapp.presenter.HealthPersonInfoPresenter
import java.util.*
import kotlin.collections.ArrayList


/**
 * Description: 健康档案-个人信息
 * @author xiangyao
 * Created by 2019/4/3 11:34 AM
 */
class PersionInfoFragment : BaseFragment<HealthPersonInfoPresenter, HealthRecordEntity.PerProfileBean>(), HealthPersonInfoContract.View {


    lateinit var mTagString: MutableList<String>

    private var uploadType: String? = null

    private val sexAraay = Arrays.asList("男", "女")
    private val blodType = Arrays.asList("A型", "B型", "AB型", "O型")
    private val marriedStatues = Arrays.asList("未婚", "已婚", "离异", "丧偶")
    private val chireldStatues = Arrays.asList("未育", "已育")
    private val workType = Arrays.asList("高级管理者", "管理人员", "营销人员", "传媒业者", "教育业者", "零售和服务业者", "科研人员", "IT业者", "文艺业者", "司机", "体力工作者", "离退休人员", "自由职业")
    private val mString6 = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8")


    override fun showDisseaseList(mString: String?) {




        mString?.let {

            val split = it.split(",")


            Observable.fromIterable(split).filter { t -> !TextUtils.isEmpty(t) }
                    .subscribe { t -> mTagString.add(t) }
        }


        if (mTagString.size > 0) {

            initFlowAdapter(mTagString)

            health_fragment_flowlayout_tv.visibility = View.GONE
            health_fragment_flowlayout.visibility = View.VISIBLE


        } else {
            health_fragment_flowlayout.visibility = View.GONE
            health_fragment_flowlayout_tv.visibility = View.VISIBLE

        }

    }


    override fun showDataSuccess(datas: HealthRecordEntity.PerProfileBean?) {


        health_fragment_tv1.text = datas?.name

        health_fragment_tv2.text = datas?.birthday


        health_fragment_tv3.text = if (datas?.sex == 0) "男" else "女"

        health_fragment_tv4.text = datas?.bloodType

        health_fragment_tv5.text = datas?.idCardNumber


        health_fragment_tv6.text = if (datas?.maritalStatusId == 0) "未婚" else "已婚"

        health_fragment_tv7.text = if (datas?.state == 0) "未育" else "已育"


        health_fragment_tv8.text = datas?.occupation

        health_fragment_tv9.text = datas?.presentAddress


    }

    override fun showDataError(errorMessage: String?) {
    }


    override fun injectTarget(): Int = R.id.root_layout

    override fun doRetry() {
    }

    override fun getLayoutResource(): Int = R.layout.fragment_persion_info

    override fun initView() {

        mTagString = ArrayList()

    }

    override fun showHeathConteacts(firstContactsLists: MutableList<HealthRecordEntity.PerProfileBean.FirstContactsListsBean>?) {

        firstContactsLists?.let {

            health_fragment_tv11.text = it[0].name
        }

    }

    override fun initData(savedInstanceState: Bundle?) {

        mPresenter.getPersionInfo()

    }

    override fun getPresenter(): HealthPersonInfoPresenter {
        return HealthPersonInfoPresenter()
    }


    private fun initFlowAdapter(diseaseString: List<String>) {


        val inflater = LayoutInflater.from(context)

        health_fragment_flowlayout.adapter = object : TagAdapter<String>(diseaseString) {

            override fun getView(parent: FlowLayout, position: Int, s: String): View {

                val textView = inflater.inflate(R.layout.flowlayout_text, health_fragment_flowlayout, false) as TextView
                textView.text = s
                textView.setOnClickListener {
                    uploadType = "illnessNames"
                }
                return textView
            }
        }
    }

    fun onClick(v: View) {


        when (v.id) {


            R.id.health_fragment_lin_btn1 -> {


                uploadType = "name"
                val title = "患者姓名"
                val hintText = "请填写身份证上的姓名"
            }

            R.id.health_fragment_lin_btn2 -> {

                uploadType = "birthday"
            }
            R.id.health_fragment_lin_btn3 -> {

                uploadType = "sex"
            }

            R.id.health_fragment_lin_btn4 -> {

                uploadType = "bloodType"


            }

            R.id.health_fragment_lin_btn5 -> {

                uploadType = "idCardNumber"
                val cardId = "身份证号"
                val hinttext4 = "请填写正确身份证号码"

            }

            R.id.health_fragment_lin_btn6 -> {


                uploadType = "maritalStatusId"
            }
            R.id.health_fragment_lin_btn7 -> {

                uploadType = "fertilityStatusId"
            }
            R.id.health_fragment_lin_btn8 -> {


                uploadType = "occupation"
            }
            R.id.health_fragment_lin_btn9 -> {

                uploadType = "presentAddress"
                val addressNew = "现住地址"
                val addressHint = "请填写详细地址"
            }
            R.id.health_fragment_lin_btn10 -> {

                uploadType = "permanentAddress"
                val addressOld = "常住地址"
                val addressHint2 = "请填写详细地址"
            }
            R.id.health_fragment_lin_btn11 -> {

                uploadType = "Sms1"
                val sms = "联系人和电话"
                val smsHint = "请填写联系人姓名"
            }
            R.id.health_fragment_lin_btn12 -> {

                uploadType = "Sms2"
                val sms2 = "联系人和电话"
                val smsHint2 = "请填写联系人姓名"
            }


            R.id.health_fragment_flowlayout_lin2 -> {

                uploadType = "illnessNames"
            }

            R.id.health_fragment_btnTv ->{}

        }

    }

}
